package cc.rengu.igas.channel.ncqs.biz.impl;


import cc.rengu.igas.channel.ncqs.common.constant.NcqsServiceConstant;
import cc.rengu.igas.channel.ncqs.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.ncqs.facade.NcqsPayService;
import cc.rengu.igas.channel.ncqs.facade.request.*;
import cc.rengu.igas.channel.ncqs.facade.response.*;
import cc.rengu.igas.channel.ncqs.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * 银联无卡快捷支付通道服务实现 on 2019/10/12.
 */
public class NcqsPayServiceImpl implements NcqsPayService {
    RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    /**
     * 协议支付签约触发短信
     *
     * @param request 协议支付签约触发短信请求
     * @return 协议支付签约触发短信响应
     */
    @Override
    public Result<NcqsAgreePaySignWithSmsAcqResponse> agreePaySignWithSms(NcqsAgreePaySignWithSmsAcqRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<NcqsAgreePaySignWithSmsAcqResponse> result = new Result<>();
        NcqsAgreePaySignWithSmsAcqResponse agreePaySignWithSmsAcqResponse = new NcqsAgreePaySignWithSmsAcqResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request,
                    NcqsServiceConstant.AGREE_PAY_WITH_SMS_SERVICE, NcqsAgreePaySignWithSmsAcqResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((NcqsAgreePaySignWithSmsAcqResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联无卡快捷协议支付签约触发短信失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                agreePaySignWithSmsAcqResponse.setRespCode(((BizException) e).getCode());
                agreePaySignWithSmsAcqResponse.setRespDesc(e.getMessage());
            } else {
                agreePaySignWithSmsAcqResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                agreePaySignWithSmsAcqResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<NcqsApplyPaySmsAcqResponse> applyPaySms(NcqsApplyPaySmsAcqRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<NcqsApplyPaySmsAcqResponse> result = new Result<>();
        NcqsApplyPaySmsAcqResponse applyPaySmsAcqResponse = new NcqsApplyPaySmsAcqResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request,
                    NcqsServiceConstant.DIRECT_PAY_WITH_SMS_SERVICE, NcqsApplyPaySmsAcqResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((NcqsApplyPaySmsAcqResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联无卡快捷直接支付签约触发短信失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                applyPaySmsAcqResponse.setRespCode(((BizException) e).getCode());
                applyPaySmsAcqResponse.setRespDesc(e.getMessage());
            } else {
                applyPaySmsAcqResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                applyPaySmsAcqResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<NcqsAcctVerifyAcqResponse> acctVerify(NcqsAcctVerifyAcqRequest request) {
        return null;
    }

    @Override
    public Result<NcqsDebitAcctSignWithSmsAcqResponse> debitAcctSignWithSms(NcqsDebitAcctSignWithSmsAcqRequest request) {
        return null;
    }

    @Override
    public Result<NcqsAgreePaySignAcqResponse> agreePaySign(NcqsAgreePaySignAcqRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<NcqsAgreePaySignAcqResponse> result = new Result<>();
        NcqsAgreePaySignAcqResponse agreePaySignAcqResponse = new NcqsAgreePaySignAcqResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request,
                    NcqsServiceConstant.AGREE_PAY_SIGN_SERVICE, NcqsAgreePaySignAcqResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((NcqsAgreePaySignAcqResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联无卡快捷协议支付签约失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                agreePaySignAcqResponse.setRespCode(((BizException) e).getCode());
                agreePaySignAcqResponse.setRespDesc(e.getMessage());
            } else {
                agreePaySignAcqResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                agreePaySignAcqResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<NcqsDebitAcctSignAcqResponse> debitAcctSign(NcqsDebitAcctSignAcqRequest request) {
        return null;
    }

    @Override
    public Result<NcqsAgreePaySignRescindAcqResponse> agreePaySignRescind(NcqsAgreePaySignRescindAcqRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<NcqsAgreePaySignRescindAcqResponse> result = new Result<>();
        NcqsAgreePaySignRescindAcqResponse agreePaySignRescindAcqResponse = new NcqsAgreePaySignRescindAcqResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request,
                    NcqsServiceConstant.AGREE_PAY_SIGN_RESCIND_SERVICE, NcqsAgreePaySignRescindAcqResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((NcqsAgreePaySignRescindAcqResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联无卡快捷协议支付解约失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                agreePaySignRescindAcqResponse.setRespCode(((BizException) e).getCode());
                agreePaySignRescindAcqResponse.setRespDesc(e.getMessage());
            } else {
                agreePaySignRescindAcqResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                agreePaySignRescindAcqResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<NcqsDebitAcctSignRescindAcqResponse> debitAcctSignRescind(NcqsDebitAcctSignRescindAcqRequest request) {
        return null;
    }

    @Override
    public Result<NcqsAgreePayAcqResponse> agreePay(NcqsAgreePayAcqRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<NcqsAgreePayAcqResponse> result = new Result<>();
        NcqsAgreePayAcqResponse agreePayAcqResponse = new NcqsAgreePayAcqResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request,
                    NcqsServiceConstant.AGREE_PAY_SERVICE, NcqsAgreePayAcqResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((NcqsAgreePayAcqResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联无卡快捷协议支付失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                agreePayAcqResponse.setRespCode(((BizException) e).getCode());
                agreePayAcqResponse.setRespDesc(e.getMessage());
            } else {
                agreePayAcqResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                agreePayAcqResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<NcqsDirectPayAcqResponse> directPay(NcqsDirectPayAcqRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<NcqsDirectPayAcqResponse> result = new Result<>();
        NcqsDirectPayAcqResponse directPayAcqResponse = new NcqsDirectPayAcqResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request,
                    NcqsServiceConstant.DIRECT_PAY_SERVICE, NcqsDirectPayAcqResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((NcqsDirectPayAcqResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联无卡快捷直接支付签约失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                directPayAcqResponse.setRespCode(((BizException) e).getCode());
                directPayAcqResponse.setRespDesc(e.getMessage());
            } else {
                directPayAcqResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                directPayAcqResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<NcqsDebitTransferAcqResponse> debitTransfer(NcqsDebitTransferAcqRequest request) {
        return null;
    }

    @Override
    public Result<NcqsCreditPayAcqResponse> creditPay(NcqsCreditPayAcqRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<NcqsCreditPayAcqResponse> result = new Result<>();
        NcqsCreditPayAcqResponse creditPayAcqResponse = new NcqsCreditPayAcqResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request,
                    NcqsServiceConstant.CREIT_PAY_SERVICE, NcqsCreditPayAcqResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((NcqsCreditPayAcqResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联无卡快捷贷记付款失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                creditPayAcqResponse.setRespCode(((BizException) e).getCode());
                creditPayAcqResponse.setRespDesc(e.getMessage());
            } else {
                creditPayAcqResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                creditPayAcqResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<NcqsRefundAcqResponse> refund(NcqsRefundAcqRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<NcqsRefundAcqResponse> result = new Result<>();
        NcqsRefundAcqResponse refundAcqResponse = new NcqsRefundAcqResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request,
                    NcqsServiceConstant.REFUND_SERVICE, NcqsRefundAcqResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((NcqsRefundAcqResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联无卡快捷退款失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                refundAcqResponse.setRespCode(((BizException) e).getCode());
                refundAcqResponse.setRespDesc(e.getMessage());
            } else {
                refundAcqResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                refundAcqResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<NcqsTransStatQueryAcqResponse> transStatQuery(NcqsTransStatQueryAcqRequest request) {
        DubboService dubboService = new DubboServiceImpl();
        Result<NcqsTransStatQueryAcqResponse> result = new Result<>();
        NcqsTransStatQueryAcqResponse transStatQueryAcqResponse = new NcqsTransStatQueryAcqResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(request,
                    NcqsServiceConstant.TRANS_STATE_QUERY_SERVICE, NcqsTransStatQueryAcqResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((NcqsTransStatQueryAcqResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银联无卡快捷协议支付签约失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                transStatQueryAcqResponse.setRespCode(((BizException) e).getCode());
                transStatQueryAcqResponse.setRespDesc(e.getMessage());
            } else {
                transStatQueryAcqResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                transStatQueryAcqResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
